package com.hqd.ch03.v20.aop.support;

import com.hqd.ch03.v20.utils.PatternMatchUtils;
import org.apache.commons.lang3.ArrayUtils;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 方法名匹配
 */
public class NameMatchMethodPointcut extends StaticMethodMatcherPointcut {
    private List<String> mappedNames = new ArrayList<>();

    public NameMatchMethodPointcut addNames(String... names) {
        if (ArrayUtils.isNotEmpty(names)) {
            this.mappedNames.addAll(Arrays.asList(names));
        }
        return this;
    }

    @Override
    public boolean matches(Method method, Class<?> targetClass) {
        for (String name : this.mappedNames) {
            if (PatternMatchUtils.simpleMatch(name, method.getName())) {
                return true;
            }
        }
        return false;
    }

    public NameMatchMethodPointcut addMethodName(String name) {
        this.mappedNames.add(name);
        return this;
    }
}
